#include "head_c.h"
//跳过空白字符的函数
int skip_space(char* buf,int* i, int* n) {
    for( ; *i < *n; (*i)++) {
        if(buf[*i] == ' ' ) {
            continue;
        }
        else if(buf[*i] == '\n') {
            return 0;
        }
        else {
            return 1;
        }
    }
    return -1;
}

//解析函数
int read_stdin(const user_t* status, portocol_t* portocol, int socket_fd) {
    char buffer[1024];
    int ret_read = read(STDIN_FILENO, buffer, sizeof(buffer));
    printf("buffer: %s ret_read = %d\n", buffer, ret_read);
    int buf_num = 0;
    int ret_skip = skip_space(buffer, &buf_num, &ret_read);
    //输入的全都是空格,说明命令输入错误,设置错误码,结束程序
    if(ret_skip == 0) {
        portocol->arg_num = 2;
        portocol->cmd = -1;
        return -1;
    }

    //读取命令
    char cmd[20] = "\0";
    char arg[1024] = "\0";
    for(int i = 0; buffer[buf_num] != ' ' && buffer[buf_num] != '\n'; i++) {
        cmd[i] = buffer[buf_num];
        buf_num++;
    }

    //判断命令类型
    if(strcmp(cmd, "1") == 0) {
        ret_skip = skip_space(buffer, &buf_num, &ret_read);
        if(ret_skip == 0) {
            portocol->cmd = LOGIN;
            return 0;
        }
        else {
            portocol->cmd = -1;
            return -1;
        }
    }
    else if(strcmp(cmd, "2") == 0) {
        ret_skip = skip_space(buffer, &buf_num, &ret_read);
        if(ret_skip == 0) {
            portocol->cmd = REGISTER;
            return 0;
        }
        else {
            portocol->cmd = -1;
            return -1;
        }
    }
    else if(strcmp(cmd, "3") == 0) {
        ret_skip = skip_space(buffer, &buf_num, &ret_read);
        if(ret_skip == 0) {
            portocol->cmd = EXIT;
            return 0;
        }
        else {
            portocol->cmd = -1;
            return -1;
        }
    }
    else if(strcmp(cmd, "cd") == 0 || 
       strcmp(cmd, "CD") == 0 ||
       strcmp(cmd, "Cd") == 0 ||
       strcmp(cmd, "cD") == 0 
      ){
        portocol->cmd = CD; 
    }
    else if(strcmp(cmd, "LS") == 0 || 
            strcmp(cmd, "ls") == 0 ||
            strcmp(cmd, "Ls") == 0 ||
            strcmp(cmd, "lS") == 0 
           ) {
        portocol->cmd = LS;
    }
    else if(strcmp(cmd, "puts") == 0 ||
            strcmp(cmd, "PUTS") == 0 
           ) {
        portocol->cmd = PUTS;
    }
    else if(strcmp(cmd, "gets") == 0 ||
            strcmp(cmd, "GETS") == 0 
           ) {
        portocol->cmd = GETS;
    }
    else if(strcmp(cmd, "rm") == 0 ||
            strcmp(cmd, "RM") == 0 ||
            strcmp(cmd, "Rm") == 0 ||
            strcmp(cmd, "rM") == 0 
           ) {
        portocol->cmd = RM;
    }
    else if(strcmp(cmd, "pwd") == 0 ||
            strcmp(cmd, "PWD") == 0 ||
            strcmp(cmd, "Pwd") == 0 ||
            strcmp(cmd, "PWd") == 0 ||
            strcmp(cmd, "pwD") == 0 ||
            strcmp(cmd, "pWd") == 0 ||
            strcmp(cmd, "PwD") == 0 
           ) {
        portocol->cmd = PWD;
    }
    else if(strcmp(cmd, "mkdir") == 0 ||
            strcmp(cmd, "MKDIR") == 0
           ) {
        portocol->cmd = MKDIR;
    }
    //判断完所有的命令,错误命令设置为1024
    else {
        portocol->cmd = -1;
        return -1;
    }
    //让buf_num指向下一个位置
    //buf_num++;
    printf("buf_num = %d\n", buf_num);

    skip_space(buffer, &buf_num, &ret_read); 
    printf("tiao buf_num = %d\n", buf_num);
    //判断是否读到stdin的末尾,读到末尾就设置参数数量为2
    if(buf_num == ret_read) {
        portocol->arg_num = 2;
        sprintf(portocol->arg, "%s&%s", status->user_name, status->path);
        int ret_send = send(socket_fd, portocol, sizeof(portocol_t), MSG_NOSIGNAL);
        //ERROR_CHECK(ret_send, -1, "read_stdin.c 107 send");
    }
    //stdin没有读到末尾,设置参数数量为3,继续读取
    else {
        portocol->arg_num = 3;
        int curr = buf_num;
        int i = 0;
        for(; i + curr < ret_read; i++ ) {
            arg[i] = buffer[buf_num];
            buf_num++;
        }
        arg[i - 1] = '\0';
        //printf("buf_num = %d\n", buf_num);
        sprintf(portocol->arg, "%s&%s&%s", status->user_name, status->path, arg);
        printf("%s\n", portocol->arg);
        int ret_send = send(socket_fd, portocol, sizeof(portocol_t), MSG_NOSIGNAL);
        //ERROR_CHECK(ret_send, -1, "read_stdin.c 107 send");
    }

    return 0;
}


